<template>
  <div class="login-container">
    <div class="login-card">
      <div class="logo">
        <h1>企业管理系统</h1>
        <p>欢迎回来，请登录您的账户</p>
      </div>

      <form>
        <div class="form-group">
          <label for="username">用户名 / 邮箱</label>
          <div class="input-with-icon">
            <el-icon>
              <User />
            </el-icon>
            <input type="text" id="username" class="form-control" :class="{ error: errors.username }"
              placeholder="请输入用户名或邮箱" v-model="form.username" @blur="validateField('username')" />
          </div>
          <div class="error-message" v-if="errors.username">
            <i class="fas fa-exclamation-circle"></i>
            {{ errors.username }}
          </div>
        </div>

        <div class="form-group">
          <label for="password">密码</label>
          <div class="input-with-icon">
            <el-icon>
              <Lock />
            </el-icon>
            <input :type="showPassword ? 'text' : 'password'" id="password" class="form-control"
              :class="{ error: errors.password }" placeholder="请输入密码" v-model="form.password"
              @blur="validateField('password')" />
            <i class="fas" :class="showPassword ? 'fa-eye-slash' : 'fa-eye'"
              style="left: auto; right: 15px; cursor: pointer" @click="showPassword = !showPassword"></i>
          </div>
          <div class="error-message" v-if="errors.password">
            <i class="fas fa-exclamation-circle"></i>
            {{ errors.password }}
          </div>
        </div>

        <div class="options">
          <div class="remember-me">
            <input type="checkbox" id="remember" v-model="form.remember" />
            <label for="remember">记住我</label>
          </div>
          <a href="#" class="forgot-password">忘记密码?</a>
        </div>
      </form>
      <button type="submit" @click="handleLogin" class="btn-login" :disabled="loading">
        <span v-if="!loading">登录</span>
        <span v-else>登录中...</span>
      </button>
      <div class="divider">或使用以下方式登录</div>

      <div class="social-login">
        <div class="social-btn wechat">
          <i class="fab fa-weixin"></i>
        </div>
        <div class="social-btn qq">
          <i class="fab fa-qq"></i>
        </div>
        <div class="social-btn weibo">
          <i class="fab fa-weibo"></i>
        </div>
      </div>

      <div class="register-link">还没有账户? <a href="#">立即注册</a></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import { User, Lock } from "@element-plus/icons-vue";
import useUserStore from "@/stores/modules/user";

let userStore = useUserStore()

const form = reactive({
  username: "sibada",
  password: "123456",
  remember: false,
});

const errors = reactive({
  username: "",
  password: "",
});

const showPassword = ref(false);
const loading = ref(false);

const validateField = (field: string) => {
  if (field === "username") {
    if (!form.username) {
      errors.username = "请输入用户名或邮箱";
    } else if (form.username.length < 3) {
      errors.username = "用户名至少3个字符";
    } else {
      errors.username = "";
    }
  }

  if (field === "password") {
    if (!form.password) {
      errors.password = "请输入密码";
    } else if (form.password.length < 6) {
      errors.password = "密码至少6个字符";
    } else {
      errors.password = "";
    }
  }
};

const validateForm = () => {
  validateField("username");
  validateField("password");

  return !errors.username && !errors.password;
};

const handleLogin = () => {
  if (!validateForm()) {
    return;
  }
  loading.value = true;
  userStore.userLogin({
    userName: form.username,
    pwd: form.password,
  })
  // 模拟登录请求
  //   setTimeout(() => {
  //     loading.value = false;
  //     alert(
  //       `登录成功!\n用户名: ${form.username}\n记住我: ${
  //         form.remember ? "是" : "否"
  //       }`
  //     );
  //   }, 1500);
};
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}

body {
  background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  padding: 20px;
}

.login-container {
  width: 100%;
  max-width: 420px;
}

.login-card {
  background: white;
  border-radius: 16px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  padding: 40px 35px;
  transition: transform 0.3s ease;
}

/* .login-card:hover {
    transform: translateY(-5px);
} */

.logo {
  text-align: center;
  margin-bottom: 30px;
}

.logo h1 {
  color: #2c3e50;
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 8px;
}

.logo p {
  color: #7f8c8d;
  font-size: 14px;
}

.form-group {
  margin-bottom: 20px;
  position: relative;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #2c3e50;
  font-weight: 500;
  font-size: 14px;
}

.input-with-icon {
  position: relative;
}

.input-with-icon i {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #7f8c8d;
}

.form-control {
  width: 100%;
  padding: 14px 15px 14px 45px;
  border: 1px solid #e1e5eb;
  border-radius: 8px;
  font-size: 15px;
  transition: all 0.3s;
}

.form-control:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
}

.form-control.error {
  border-color: #e74c3c;
}

.error-message {
  color: #e74c3c;
  font-size: 13px;
  margin-top: 5px;
  display: flex;
  align-items: center;
}

.error-message i {
  margin-right: 5px;
}

.options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
}

.remember-me {
  display: flex;
  align-items: center;
}

.remember-me input {
  margin-right: 8px;
}

.forgot-password {
  color: #3498db;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s;
}

.forgot-password:hover {
  color: #2980b9;
  text-decoration: underline;
}

.btn-login {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, #3498db, #2980b9);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 4px 15px rgba(52, 152, 219, 0.3);
}

.btn-login:hover {
  background: linear-gradient(135deg, #2980b9, #3498db);
  box-shadow: 0 6px 20px rgba(52, 152, 219, 0.4);
  /* transform: translateY(-2px); */
}

/* .btn-login:active {
    transform: translateY(0);
} */

.divider {
  text-align: center;
  margin: 25px 0;
  position: relative;
  color: #95a5a6;
  font-size: 14px;
}

.divider::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  width: 42%;
  height: 1px;
  background: #e1e5eb;
}

.divider::after {
  content: "";
  position: absolute;
  right: 0;
  top: 50%;
  width: 42%;
  height: 1px;
  background: #e1e5eb;
}

.social-login {
  display: flex;
  justify-content: center;
  gap: 15px;
  margin-bottom: 25px;
}

.social-btn {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 18px;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

.social-btn:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 15px rgba(0, 0, 0, 0.15);
}

.social-btn.wechat {
  background: #09bb07;
}

.social-btn.qq {
  background: #12b7f5;
}

.social-btn.weibo {
  background: #e6162d;
}

.register-link {
  text-align: center;
  color: #7f8c8d;
  font-size: 14px;
}

.register-link a {
  color: #3498db;
  text-decoration: none;
  font-weight: 500;
  margin-left: 5px;
}

.register-link a:hover {
  text-decoration: underline;
}

/* @media (max-width: 480px) {
    .login-card {
        padding: 30px 25px;
    }

    .options {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
    }

    .forgot-password {
        align-self: flex-end;
    }
} */
</style>
